$(document).ready(function() {

	function msg_success() {
		$("#output").html('<div class=success>command successfuly posted!</div>');
	}

	function msg_fail() {
		$("#output").html('<div class=fail>command failed!</div>');
	}

	function send() {
		alert('asd');
		$("#output").html('adsad');
		return false;
	}

	$("#btn").click(function() {
		//alert('got in');		
		var query = "DanielPark";
		$.get("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20search.web%20where%20query%3D%22"+query+"%22&format=json&debug=true",
			function (data) {
				var size = data.query.results.result.length;
				for(var i = 0; i < size; i++){
					var res = data.query.results.result[i];
					var url = res.url;
					var title = res.title;
					var desc = res.abstract;
					$("#output").append(title + "<br>");
					$("#output").append(desc + "<br>");
					$("#output").append("<a href='"+url+"' target=_blank>"+url+"</a><br><br>");
					//alert(data.query.results.item[0].title);
					console.log(data);
				}
			}, 
			"json");
	
		return false;
	});
	
	$("#vidbtn").click(function() {
		//alert('got in');		
		var query = "DanielPark";
		$.get("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20youtube.search%20where%20query%3D%22"+query+"%22&diagnostics=true&env=store://datatables.org/alltableswithkeys&format=json&debug=true",
			function (data) {
				var size = data.query.results.video.length;
				size = (size > 5) ? 5 : size;
				for(var i = 0; i < size; i++){
					var vid = data.query.results.video[i];
					var url = vid.url;
					var title = vid.title;
					var desc = vid.content;
					var auth = vid.author;
					var id = vid.id;
					$("#output").append(title + "<br>");
					$("#output").append(auth + "<br>");
					$("#output").append(desc + "<br>");
					$("#output").append("<a href='"+url+"' target=_blank>"+url+"</a><br><br>");
					//alert(data.query.results.item[0].title);
					console.log(data);
					
					$("#output").append("<iframe title='YouTube video player' width='640' height='390' src='http://www.youtube.com/embed/"+ id +"' frameborder='0' allowfullscreen></iframe><br>");
				}
			}, 
			"json");
	
		return false;
	});
});
